package cn.edu.zucc.service.impl;

//import cn.edu.zucc.dao.StuMapper;
import cn.edu.zucc.anoDao.StuMapper;
import cn.edu.zucc.entity.Stu;
import cn.edu.zucc.repository.StuRepository;
import cn.edu.zucc.service.StuService;
import cn.edu.zucc.service.UserService;
import cn.edu.zucc.util.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//public class UserServiceImpl {
//}

//package com.example.demo.service;

//        import com.example.demo.entity.User;
//        import com.example.demo.repository.UserRepository;


/**
 * Copyright (C), 2019-2019, XXX有限公司
 * FileName: UserServiceImpl
 * Author:   longzhonghua
 * Date:     3/27/2019 2:26 PM
 * Description: ${DESCRIPTION}
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
//@Service
//public class StuServiceImpl implements StuService {
//    @Resource
//    private StuRepository stuRepository;
//
////    public Flux<Stu> getAll() {
//    public List<Stu> getAll() {
////        return stuRepository.findAll();
//        List<Stu> all = stuRepository.findAll();
//        return  all;
//    }
//}


/**
 * @description stu
 * @author zhengkai.blog.csdn.net
 * @date 2022-04-13
 */
@Service
public class StuServiceImpl implements StuService {

    @Resource
    private StuMapper stuMapper;


    @Override
    public Object insert(Stu stu) {

        // valid
        if (stu == null) {
            return ReturnT.error("必要参数缺失");
        }

        stuMapper.insert(stu);
        return ReturnT.success();
    }


    @Override
    public Object delete(int id) {
        int ret = stuMapper.delete(id);
        return ret>0?ReturnT.success():ReturnT.error();
    }


    @Override
    public Object update(Stu stu) {
        int ret = stuMapper.update(stu);
        return ret>0?ReturnT.success():ReturnT.error();
    }


    @Override
//    public Stu load(int id) {
//        return stuMapper.load(id);
//    }
    public Stu load(int id) {
        return stuMapper.getById(id);
    }

    @Override
    public Object save(Stu stu) {
        Integer id = stu.getId();
        if (id==null) {
            int insert = stuMapper.insert(stu);
            return ReturnT.success();
        }
        Stu load = stuMapper.getById(stu.getId());
        if (load==null) {
            int insert = stuMapper.insert(stu);

            return ReturnT.success();
        }
        stuMapper.update(stu);
        return  ReturnT.success();
    }


    @Override
    public Map<String,Object> pageList(int offset, int pageSize) {

        List<Stu> pageList = stuMapper.pageList(offset, pageSize);
        Integer totalCount = stuMapper.pageListCount(offset, pageSize);

//        stuMapper.
        // result
        Map<String, Object> result = new HashMap<String, Object>();
        //result.put("pageList", pageList);
        result.put("content", pageList);
        result.put("totalCount", totalCount);

        return result;
    }

}
